package com.orhon.smartcampus.modules.material.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.material.dao.UnitDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLUnit;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.UnitMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class UnitFetcher implements DataFetcher  {
	
	@Autowired
	private UnitDao unitDao;
	
	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Unit where deleted_at = null and id = "+id;
		Unit item =  unitDao.findId(hql, null);
		if(item!=null) {
			GQLBuilding gqlBuilding = null;
			if (env.getSelectionSet().contains("building")){
				Buildings buildings = item.getMaterialBuildingsByBuildingsId();
				 gqlBuilding = BuildingMapper.MAPPER.toBuilding(buildings);
			}
			
			GQLSchool GgqlSchool = null;
 			if (env.getSelectionSet().contains("school")){
 				Schools schools = item.getBaseSchoolsBySchoolId();
 				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
 			}

			GQLUnit unit = UnitMapper.MAPPER.toUnit(item);
			unit.setBuilding(gqlBuilding);
			unit.setSchool(GgqlSchool);
			return unit;
		}
		return null;
	}
}
